package com.itheima.mp.controller;

import cn.hutool.core.bean.BeanUtil;
import com.itheima.mp.domain.dto.PageDTO;
import com.itheima.mp.enums.UserStatus;
import com.itheima.mp.domain.dto.UserFormDTO;
import com.itheima.mp.domain.po.Address;
import com.itheima.mp.domain.po.ResultType;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.query.UserQuery;
import com.itheima.mp.domain.vo.UserAddressVo;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.service.IAddressService;
import com.itheima.mp.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
@RequiredArgsConstructor // 构造函数注入，对需要初始化的final内容初始化
public class UserController {

    private final IUserService userService;
    private final IAddressService addressService;
//    // 通过构造函数注入，避免Autowired注入
//    UserController(IUserService userService){
//        this.userService = userService;
//    }

    @PostMapping
    @ApiOperation("新增用户接口")
    public ResultType<Integer> insertUser(@RequestBody UserFormDTO userDTO){
        User user = new User();
        BeanUtils.copyProperties(userDTO, user);
        return new ResultType<Integer>(200, userService.save(user) ? 1 : 0, "新增成功");
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除用户接口")
    public ResultType<Integer> deleteById(@PathVariable Long id){
        return new ResultType<Integer>(200, userService.removeById(id) ? 1 : 0, "删除成功");
    }

    @GetMapping("/{id}")
    @ApiOperation("查询用户接口")
    public UserVO selectById(@PathVariable Long id){
        return userService.getUserVoById(id);
    }

    @GetMapping
    @ApiOperation("批量查询用户接口")
    public List<UserVO> selectById(@RequestParam List<Long> ids){
        return userService.getUserVoByIds(ids);
    }

    @PutMapping("/{id}/deduction/{money}")
    @ApiOperation("用户余额扣减接口")
    public ResultType<Integer> updateMoney(
            @PathVariable("id") Long id,
            @PathVariable("money") Integer money
    ) {
        return new ResultType<>(200, userService.updateMoneyById(id, money), "扣减成功");
    }

    @GetMapping("/query")
    @ApiOperation("批量查询用户接口")
    public List<UserVO> selectByInfo(UserQuery userQuery){
        return userService.getUserVoByUserQuery(userQuery);
    }

    @GetMapping("/address/{id}")
    @ApiOperation("根据id查询地址")
    public UserAddressVo selectUserAddressById(@PathVariable Long id) {
        User user = userService.getById(id);
        UserAddressVo userAddressVo= BeanUtil.copyProperties(user, UserAddressVo.class);
        if (user != null) {
            if(user.getStatus() != UserStatus.NORMAL) throw new RuntimeException("用户状态异常");
            List<Address> list = addressService.lambdaQuery().eq(Address::getUserId, id).list();
            userAddressVo.setAddressList(list);
        }
        else throw new RuntimeException("用户不存在");
        return userAddressVo;
    }

    @GetMapping("/address")
    @ApiOperation("根据ids批量查询地址")
    public List<UserAddressVo> selectUserAddressByIds(@RequestParam List<Long> ids) {
        List<UserAddressVo> userAddressVoList = userService.getUserAddressVoByIds(ids);
        return userAddressVoList;
    }

    @GetMapping("/page")
    @ApiOperation("分页查询用户接口")
    public PageDTO<UserVO> selectPage(UserQuery userQuery){
        return userService.getPageQuery(userQuery);
    }
}
